package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @PostMapping
    public Result addBusiness(@RequestBody Business  business) {
    log.info("添加：{}",business );
    business.setUpdateTime(LocalDateTime.now());
    business.setCreateTime(LocalDateTime.now());
    business.setStatus(1);
    businessService.save(business);
    return Result.success();
    }
    @GetMapping
    public Result list(BusinessDto businessDto){
        PageResult<Business> pageResult =businessService.listBusiness(businessDto) ;
        return Result.success(pageResult);
    }
    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId,@PathVariable Integer userId){
        log.info("businessId:{},userId:{}",businessId,userId);
        Business business = new Business();
        business.setId(businessId);
        business.setStatus(2);
        business.setUserId(userId);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    /**
     * 根据id查询商机
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer  id){
        log.info("查询商机id:{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    /**
     * 跟进商机
     */
    @PutMapping
    public Result trackBusiness(@RequestBody Business  business){
        log.info("跟进商机:{}", business);
        businessService.trackBusiness(business);
        return Result.success();
    }
    //提回公海
    @PutMapping("/back/{id}")
    public Result backBusiness(@PathVariable Integer id){
        log.info("提回公海id:{}", id);
        businessService.backBusiness(id);
        return Result.success();
    }
    /**
     * 转客户
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户id:{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result selectPoolBusiness(BusinessDto businessDto) {
        log.info("查询公海商机");
        PageResult<Business> pageResult = businessService.poolBusiness(businessDto);
        return Result.success(pageResult);
    }
}
